home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JRDTARGA.C < prev    next >
C/C++ Source or Header  |  1992-12-02  |  14KB  |  471 lines

  1. /*
  2.  * jrdtarga.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to read input images in Targa format.
  9.  *
  10.  * These routines may need modification for non-Unix environments or
  11.  * specialized applications.  As they stand, they assume input from
  12.  * an ordinary stdio stream.  They further assume that reading begins
  13.  * at the start of the file; input_init may need work if the
  14.  * user interface has already read some data (e.g., to determine that
  15.  * the file is indeed Targa format).
  16.  *
  17.  * These routines are invoked via the methods get_input_row
  18.  * and input_init/term.
  19.  *
  20.  * Based on code contributed by Lee Daniel Crocker.
  21.  */
  22.  
  23. #include "jinclude.h"
  24.  
  25. #ifdef TARGA_SUPPORTED
  26.  
  27.  
  28. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  29.  
  30. #ifdef HAVE_UNSIGNED_CHAR
  31. typedef unsigned char U_CHAR;
  32. #define UCH(x)    ((int) (x))
  33. #else /* !HAVE_UNSIGNED_CHAR */
  34. #ifdef CHAR_IS_UNSIGNED
  35. typedef char U_CHAR;
  36. #define UCH(x)    ((int) (x))
  37. #else
  38. typedef char U_CHAR;
  39. #define UCH(x)    ((int) (x) & 0xFF)
  40. #endif
  41. #endif /* HAVE_UNSIGNED_CHAR */
  42.  
  43.  
  44. #define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  45.  
  46.  
  47. static JSAMPARRAY colormap;    /* Targa colormap (converted to my format) */
  48.  
  49. static big_sarray_ptr whole_image; /* Needed if funny input row order */
  50. static long current_row;    /* Current logical row number to read */
  51.  
  52. /* Pointer to routine to extract next Targa pixel from input file */
  53. static void (*read_pixel) PP((compress_info_ptr cinfo));
  54.  
  55. /* Result of read_pixel is delivered here: */
  56. static U_CHAR tga_pixel[4];
  57.  
  58. static int pixel_size;        /* Bytes per Targa pixel (1 to 4) */
  59.  
  60. /* State info for reading RLE-coded pixels; both counts must be init to 0 */
  61. static int block_count;        /* # of pixels remaining in RLE block */
  62. static int dup_pixel_count;    /* # of times to duplicate previous pixel */
  63.  
  64. /* This saves the correct pixel-row-expansion method for preload_image */
  65. static void (*get_pixel_row) PP((compress_info_ptr cinfo,
  66.                  JSAMPARRAY pixel_row));
  67.  
  68.  
  69. /* For expanding 5-bit pixel values to 8-bit with best rounding */
  70.  
  71. static const UINT8 c5to8bits[32] = {
  72.     0,   8,  16,  24,  32,  41,  49,  57,
  73.    65,  74,  82,  90,  98, 106, 115, 123,
  74.   131, 139, 148, 156, 164, 172, 180, 189,
  75.   197, 205, 213, 222, 230, 238, 246, 255
  76. };
  77.  
  78.  
  79.  
  80. LOCAL int
  81. read_byte (compress_info_ptr cinfo)
  82. /* Read next byte from Targa file */
  83. {
  84.   register FILE *infile = cinfo->input_file;
  85.   register int c;
  86.  
  87.   if ((c = getc(infile)) == EOF)
  88.     ERREXIT(cinfo->emethods, "Premature EOF in Targa file");
  89.   return c;
  90. }
  91.  
  92.  
  93. LOCAL void
  94. read_colormap (compress_info_ptr cinfo, int cmaplen, int mapentrysize)
  95. /* Read the colormap from a Targa file */
  96. {
  97.   int i;
  98.  
  99.   /* Presently only handles 24-bit BGR format */
  100.   if (mapentrysize != 24)
  101.     ERREXIT(cinfo->emethods, "Unsupported Targa colormap format");
  102.  
  103.   for (i = 0; i < cmaplen; i++) {
  104.     colormap[2][i] = (JSAMPLE) read_byte(cinfo);
  105.     colormap[1][i] = (JSAMPLE) read_byte(cinfo);
  106.     colormap[0][i] = (JSAMPLE) read_byte(cinfo);
  107.   }
  108. }
  109.  
  110.  
  111. /*
  112.  * read_pixel methods: get a single pixel from Targa file into tga_pixel[]
  113.  */
  114.  
  115. LOCAL void
  116. read_non_rle_pixel (compress_info_ptr cinfo)
  117. /* Read one Targa pixel from the input file; no RLE expansion */
  118. {
  119.   register FILE * infile = cinfo->input_file;
  120.   register int i;
  121.  
  122.   for (i = 0; i < pixel_size; i++) {
  123.     tga_pixel[i] = (U_CHAR) getc(infile);
  124.   }
  125. }
  126.  
  127.  
  128. LOCAL void
  129. read_rle_pixel (compress_info_ptr cinfo)
  130. /* Read one Targa pixel from the input file, expanding RLE data as needed */
  131. {
  132.   register FILE * infile = cinfo->input_file;
  133.   register int i;
  134.  
  135.   /* Duplicate previously read pixel? */
  136.   if (dup_pixel_count > 0) {
  137.     dup_pixel_count--;
  138.     return;
  139.   }
  140.  
  141.   /* Time to read RLE block header? */
  142.   if (--block_count < 0) {    /* decrement pixels remaining in block */
  143.     i = read_byte(cinfo);
  144.     if (i & 0x80) {        /* Start of duplicate-pixel block? */
  145.       dup_pixel_count = i & 0x7F; /* number of duplications after this one */
  146.       block_count = 0;        /* then read new block header */
  147.     } else {
  148.       block_count = i & 0x7F;    /* number of pixels after this one */
  149.     }
  150.   }
  151.  
  152.   /* Read next pixel */
  153.   for (i = 0; i < pixel_size; i++) {
  154.     tga_pixel[i] = (U_CHAR) getc(infile);
  155.   }
  156. }
  157.  
  158.  
  159. /*
  160.  * Read one row of pixels.
  161.  *
  162.  * We provide several different versions depending on input file format.
  163.  */
  164.  
  165.  
  166. METHODDEF void
  167. get_8bit_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  168. /* This version is for reading 8-bit grayscale pixels */
  169. {
  170.   register JSAMPROW ptr0;
  171.   register long col;
  172.   
  173.   ptr0 = pixel_row[0];
  174.   for (col = cinfo->image_width; col > 0; col--) {
  175.     (*read_pixel) (cinfo);    /* Load next pixel into tga_pixel */
  176.     *ptr0++ = (JSAMPLE) UCH(tga_pixel[0]);
  177.   }
  178. }
  179.  
  180. METHODDEF void
  181. get_8bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  182. /* This version is for reading 8-bit colormap indexes */
  183. {
  184.   register int t;
  185.   register JSAMPROW ptr0, ptr1, ptr2;
  186.   register long col;
  187.   
  188.   ptr0 = pixel_row[0];
  189.   ptr1 = pixel_row[1];
  190.   ptr2 = pixel_row[2];
  191.   for (col = cinfo->image_width; col > 0; col--) {
  192.     (*read_pixel) (cinfo);    /* Load next pixel into tga_pixel */
  193.     t = UCH(tga_pixel[0]);
  194.     *ptr0++ = colormap[0][t];
  195.     *ptr1++ = colormap[1][t];
  196.     *ptr2++ = colormap[2][t];
  197.   }
  198. }
  199.  
  200. METHODDEF void
  201. get_16bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  202. /* This version is for reading 16-bit pixels */
  203. {
  204.   register int t;
  205.   register JSAMPROW ptr0, ptr1, ptr2;
  206.   register long col;
  207.   
  208.   ptr0 = pixel_row[0];
  209.   ptr1 = pixel_row[1];
  210.   ptr2 = pixel_row[2];
  211.   for (col = cinfo->image_width; col > 0; col--) {
  212.     (*read_pixel) (cinfo);    /* Load next pixel into tga_pixel */
  213.     t = UCH(tga_pixel[0]);
  214.     t += UCH(tga_pixel[1]) << 8;
  215.     /* We expand 5 bit data to 8 bit sample width.
  216.      * The format of the 16-bit (LSB first) input word is
  217.      *     xRRRRRGGGGGBBBBB
  218.      */
  219.     *ptr2++ = (JSAMPLE) c5to8bits[t & 0x1F];
  220.     t >>= 5;
  221.     *ptr1++ = (JSAMPLE) c5to8bits[t & 0x1F];
  222.     t >>= 5;
  223.     *ptr0++ = (JSAMPLE) c5to8bits[t & 0x1F];
  224.   }
  225. }
  226.  
  227. METHODDEF void
  228. get_24bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  229. /* This version is for reading 24-bit pixels */
  230. {
  231.   register JSAMPROW ptr0, ptr1, ptr2;
  232.   register long col;
  233.   
  234.   ptr0 = pixel_row[0];
  235.   ptr1 = pixel_row[1];
  236.   ptr2 = pixel_row[2];
  237.   for (col = cinfo->image_width; col > 0; col--) {
  238.     (*read_pixel) (cinfo);    /* Load next pixel into tga_pixel */
  239.     *ptr0++ = (JSAMPLE) UCH(tga_pixel[2]); /* convert BGR to RGB order */
  240.     *ptr1++ = (JSAMPLE) UCH(tga_pixel[1]);
  241.     *ptr2++ = (JSAMPLE) UCH(tga_pixel[0]);
  242.   }
  243. }
  244.  
  245. /*
  246.  * Targa also defines a 32-bit pixel format with order B,G,R,A.
  247.  * We presently ignore the attribute byte, so the code for reading
  248.  * these pixels is identical to the 24-bit routine above.
  249.  * This works because the actual pixel length is only known to read_pixel.
  250.  */
  251.  
  252. #define get_32bit_row  get_24bit_row
  253.  
  254.  
  255. /*
  256.  * This method is for re-reading the input data in standard top-down
  257.  * row order.  The entire image has already been read into whole_image
  258.  * with proper conversion of pixel format, but it's in a funny row order.
  259.  */
  260.  
  261. METHODDEF void
  262. get_memory_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  263. {
  264.   JSAMPARRAY image_ptr;
  265.   long source_row;
  266.  
  267.   /* Compute row of source that maps to current_row of normal order */
  268.   /* For now, assume image is bottom-up and not interlaced. */
  269.   /* NEEDS WORK to support interlaced images! */
  270.   source_row = cinfo->image_height - current_row - 1;
  271.  
  272.   /* Fetch that row from virtual array */
  273.   image_ptr = (*cinfo->emethods->access_big_sarray)
  274.         (whole_image, source_row * cinfo->input_components, FALSE);
  275.  
  276.   jcopy_sample_rows(image_ptr, 0, pixel_row, 0,
  277.             cinfo->input_components, cinfo->image_width);
  278.  
  279.   current_row++;
  280. }
  281.  
  282.  
  283. /*
  284.  * This method loads the image into whole_image during the fi